package com.inviscidlabs.schooled;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;


public class DialogFragmentDefault extends DialogFragment{
	
	
	private Context mContext;
	private DefaultDialogResult dDR;
	
	private String mTitle;
	private String mMessage;
	private String mPositive;
	private String mNegative;
	private boolean hNegative;
	
	
	public DialogFragmentDefault(Context ctx,String title, String message, String positive, String negative, boolean hasNegative){
		mContext=ctx;
		mTitle=title; mMessage=message; mPositive=positive; mNegative=negative; hNegative=hasNegative;
		
	}
	
	
	
	
	
//==========================On Create View==================
	/*@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
		      Bundle savedInstanceState){
		super.onCreateView(inflater, container, savedInstanceState);
		View v = inflater.inflate(R.layout.dialog_default, container);
		tv_title=(TextView) v.findViewById(R.id.dialog_default_title);
		tv_message=(TextView) v.findViewById(R.id.dialog_default_message);
		btn_positive=(Button) v.findViewById(R.id.dialog_default_positive);
		btn_negative=(Button) v.findViewById(R.id.dialog_default_negative);
		return v;
	}*/
	
	
//=======================Create Dialog=============================

	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		//TODO include a theme
		AlertDialog.Builder aDB= new AlertDialog.Builder(mContext);
		aDB.setMessage(mMessage);
		aDB.setTitle(mTitle);
		aDB.setPositiveButton(mPositive,new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dDR.positiveReturned();
				
			}
		});
		//If we said we have a negative button, set it's text and onClickListener
		if(hNegative==true){
		aDB.setNegativeButton(mNegative, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dDR.negativeReturned();
				
			}
		});}
		
		return aDB.create();
		
	}
	
//===============Interface for Buttons=============
    
    public interface DefaultDialogResult{
    	public void positiveReturned();
    	public void negativeReturned();
    }
    
    
//=============ON ATTACH============
    
    @Override
	  public void onAttach(Activity activity) {
	    super.onAttach(activity);
	    if (activity instanceof DefaultDialogResult){
	    	dDR= (DefaultDialogResult) activity;
	    } else {
	    	 throw new ClassCastException(activity.toString()+"must implement DefaultDialogResult"); 
	    }
    
    }
	

}
